package com.jwp.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class StringUtil {
	public static int cou =1;
	public  static String NulltoBlank(Object o){
		if(o==null||"".equals(o.toString().trim())){
			return "";
		}
		return o.toString();
	}
	
	public static  String MoreStringToOneWithDou(String [] st){
		if(st==null || st.length==0){
			return "";
		}
		if(st.length==1){
			return st[0];
		}
		StringBuffer bf = new StringBuffer();
		for(String s : st){
			bf.append(s).append(",");
		}
		String res = bf.toString();
		if(res.endsWith(",")){
			res = res.substring(0,res.length()-1);
		}
		return res;
	}
	
	public static  String NulltoZero(Object o){
		if(o==null||"".equals(o.toString().trim())){
			return "0";
		}
		return o.toString();
	}
	
	public static String getOneStringByRegex(String con,String reg){
		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(con);
		if(m.matches()){
			return m.group(1);
		}
		return "";
	}
	
	/**
	 * 读取流并拼接转换成字符串
	 * 
	 * @param is
	 *            流
	 * @param encode 字符集编码 null的时候为UTF-8
	 * @return 转换后的字符串
	 * @throws IOException
	 */
	public static String readInputStream(InputStream is,String encode) throws IOException {
		if(encode == null){
			encode = "UTF-8";
		}
		BufferedReader in = new BufferedReader(new InputStreamReader(is, encode));
		StringBuffer buffer = new StringBuffer();
		String line;
		while ((line = in.readLine()) != null) {
			buffer.append(line);
		}
		is.close();
		return buffer.toString();
	}
	
	public static String formatText(String text,Object o){
		MessageFormat mf = new MessageFormat(text);
		return mf.format(o);
	}
	
	 /**
     * 使用正则匹配字符串
     * 
     * @param regex
     *            正则表达式
     * @param txt
     *            要验证的字符串
     * @return 匹配则返回真 否则返回假
     */
    public static boolean useRegex(String regex, String txt) {
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(txt);
        return m.matches();
    }

    /**
     * 使用正则匹配字符串
     * 
     * @param regex
     *            正则表达式 *
     * @param index
     *            要取第几个元素
     * @param txt
     *            要验证的字符串
     * @return 返回匹配的字符串
     */
    public static String getByRegex(String regex, int index, String txt) {
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(txt);
        if (m.find()) {
            return m.group(index);
        }
        return null;
    }
    
    /**
     * 使用正则匹配字符串
     * 
     * @param regex
     *            正则表达式 *
     * @param index
     *            要取第几个元素
     * @param txt
     *            要验证的字符串
     * @return 返回匹配的字符串数组
     */
    public static String [] getStringsByRegex(String regex, int [] index, String txt) {
        String res [] = new String[index.length];
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(txt);
        if (m.find()) {
            for(int i : index){
                res[i] = m.group(i);
            }
        }
        return res;
    }
}
